package com.example.springboot.mybatisplus.util;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.baomidou.mybatisplus.generator.fill.Property;

import static com.baomidou.mybatisplus.generator.config.TemplateType.XML;

/**
 * @author lei yu
 * @date 2024/05/17
 */
public class AutoGeneratorUtils {
    private static final String URL = "jdbc:mysql://localhost:3306/demo?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8";
    private static final String USERNAME = "admin";
    private static final String PASSWORD = "admin@me4";
    private static final String AUTHOR = "Lei Yu";
    private static final String OUT = System.getProperty("user.dir") + "/src/main";

    private static final String OUT_JAVA = OUT + "/java";
    private static final String OUT_XML = OUT + "/resources/mapper";
    private static final String PARENT = "com.example.springboot.mybatisplus";

    public static void main(String[] args) {
        new AutoGenerator(new DataSourceConfig.Builder(URL, USERNAME, PASSWORD).build())
                .global(new GlobalConfig.Builder()
                                // 作者
                                .author(AUTHOR)
                                // 时间类型：TIME_PACK => LocalDateTime
                                .dateType(DateType.TIME_PACK)
                                // 输出：./src/main/java
                                .outputDir(OUT_JAVA).build()
                )
                .packageInfo(new PackageConfig.Builder()
                                .parent(PARENT)
//                        .moduleName("sys")
                                .entity("dal.entity")
                                .mapper("dal.mapper")
                                .xml("mapper.xml")
                                .service("service")
                                .serviceImpl("service.impl")
                                .controller("controller")
//                        .pathInfo(Collections.singletonMap(OutputFile.xml, OUT_XML))
                                .build()
                )
                .strategy(new StrategyConfig.Builder()
                                // 包含哪些表
                                .addInclude("emp")
//                                .addTablePrefix("t_")
                                // Entity 策略配置
                                .entityBuilder()
//                                .superClass(BaseEntity.class)
//                                .addSuperEntityColumns("id", "created_by", "created_time", "updated_by", "updated_time")
                                .addSuperEntityColumns("created_time")
                                .disableSerialVersionUID()
                                .enableChainModel()
                                .enableLombok()
                                .enableRemoveIsPrefix()
//                                .enableTableFieldAnnotation()
//                                .enableActiveRecord()
                                .versionColumnName("version")
                                .versionPropertyName("version")
                                .logicDeleteColumnName("deleted")
                                .logicDeletePropertyName("deleteFlag")
                                .naming(NamingStrategy.underline_to_camel)
                                .columnNaming(NamingStrategy.underline_to_camel)
                                .addIgnoreColumns("age")
                                .addTableFills(new Column("create_time", FieldFill.INSERT))
                                .addTableFills(new Property("updateTime", FieldFill.INSERT_UPDATE))
                                .idType(IdType.AUTO)
                                .formatFileName("%s")
                                // Controller 策略配置
                                .controllerBuilder()
                                // Mapper 策略配置
                                .mapperBuilder()
//                                .superClass(BaseMapperX.class)
                                .build()
                )

                // 不生成 xml文件
                .template(new TemplateConfig.Builder().disable(XML).build())
                .execute();
    }
}
